package com.finalist.util;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

/**
 * 配置文件读取类
 * @author pengyb
 *
 */
public class ConstConfigUtil
{

    /**
     * 获取某配置文件中配置信息
     * @param key  配置信息的key名
     * @param constConfigFileName 配置文件名
     * @return
     */
    public static String getString(String key, String constConfigFileName)
    {
        Map<String, String> map = getAll(constConfigFileName);
        if (map == null || map.size() < 1)
        {
            return null;
        }
        return map.get(key);
    }

    /**
     * 获取配置文件中的所有配置信息
     * @param constConfigFileName 配置文件名
     * @return
     */
    public static Map<String, String> getAll(String constConfigFileName)
    {
        Logger log = Logger.getLogger(ConstConfigUtil.class);
        Map<String, String> map = null;
        try
        {
            map = new HashMap<String, String>();
            log.info("开始读取" + constConfigFileName + ".properties配置文件");
            ResourceBundle constConfig = ResourceBundle.getBundle(constConfigFileName);
            Enumeration<String> en = constConfig.getKeys();
            while (en.hasMoreElements())
            {
                String key = en.nextElement();
                map.put(key, constConfig.getString(key));
            }
            log.info("结束读取" + constConfigFileName + ".properties配置文件");
        }
        catch (Exception e)
        {
            log.error("读取" + constConfigFileName + ".properties配置文件出错", e);
        }
        return map;
    }

    public static void main(String[] args)
    {
        System.out.println(getString("system.code", "auth"));
    }
}
